<?php
/**
 * acts as the subject
 */
class InvoiceComponent implements SplSubject {
	protected $_observers;
	
	protected $_product;
	protected $_quantity;
	
	/**
	 * class constructs
	 * @param object $_product
	 * @param object $_quantity
	 * @return 
	 */
	public function __construct($_product, $_quantity) {
		$this->_product = $_product;
		$this->_quantity = $_quantity;
		$this->_attachDefaultObservers();
	}
	
	/**
	 * adds observers
	 * @return 
	 */
	protected function _attachDefaultObservers() {
		$this->attach(new Observers_DeliveryLabels());
		$this->attach(new Observers_GenerateWaranty());
		$this->attach(new Observers_UpdateStock());
	}
	
	/**
	 * adds an observer
	 * @param SplObserver $observer
	 * @return 
	 */
	public function attach(SplObserver $observer) {
		$id = spl_object_hash($observer);
		$this->_observers[$id] = $observer;
	}
	
	/**
	 * 
	 * @param SplObserver $observer
	 * @return 
	 */
	public function detach(SplObserver $observer) {
		$id = spl_object_hash($observer);
		unset($this->_observers[$id]);
	}
	
	/**
	 * notifies the observers
	 * @return 
	 */
	public function notify() {
		foreach($this->_observers as $observer) {
			$observer->update($this);
		}
	}
	
	/**
	 * perform the sell and notify the observers
	 * @return 
	 */
	public function sell() {
		$this->notify();
	}
	
	/**
	 * returns product
	 * @return string
	 */
	public function getProduct() {
		return $this->_product;
	}
	
	/**
	 * returns selled quantity
	 * @return int
	 */
	public function getQuantity() {
		return $this->_quantity;
	}
	
	/**
	 * sets the product
	 * @param string $_product
	 * @return 
	 */
	public function setProduct($_product) {
		$this->_product = $_product;
	}
	
	/**
	 * sets the quantity
	 * @param int $_quantity
	 * @return 
	 */
	public function setQuantity($_quantity) {
		$this->_quantity = $_quantity;
	}
}